package top.mcwebsite.playandroid.business.search.impl.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import top.mcwebsite.playandroid.business.search.impl.model.HotSearchKey
import top.mcwebsite.playandroid.business.search.impl.model.SearchHistoryKey
import top.mcwebsite.playandroid.business.search.impl.vm.SearchIntent
import top.mcwebsite.playandroid.business.search.impl.vm.SearchUiState
import top.mcwebsite.playandroid.common.flex.SimpleFlexRow
import top.mcwebsite.playandroid.lib.mvi.model.Async
import top.mcwebsite.playandroid.strings.Str

@Composable
fun SearchIntroPage(
    uiState: SearchUiState,
    sendIntent: (SearchIntent) -> Unit,
) {
    Column {
        SearchHistory(uiState.searchHistoryData) {
            sendIntent(SearchIntent.UpdateSearchKeywordIntent(keyword = it.key))
            sendIntent(SearchIntent.SearchActionIntent)
        }
        SearchHotKey(searchHotKeys = uiState.hotSearchKeys) { hotSearchKey ->
            sendIntent(SearchIntent.UpdateSearchKeywordIntent(keyword = hotSearchKey.name))
            sendIntent(SearchIntent.SearchActionIntent)
        }
    }
}

@Composable
fun SearchHistory(
    searchHistoryKeys: List<SearchHistoryKey>,
    onClick: (SearchHistoryKey) -> Unit
) {
    if (searchHistoryKeys.isEmpty()) {
        return
    }
    Column(
        modifier = Modifier
            .padding(horizontal = 12.dp)
            .padding(bottom = 20.dp)
    ) {
        Text(text = stringResource(id = Str.search_history), modifier = Modifier.padding(bottom = 10.dp))
        SearchHistoryKeyContent(searchHistoryKeys) {
            onClick(it)
        }
    }
}


@Composable
private fun SearchHistoryKeyContent(
    searchHotKeys: List<SearchHistoryKey>,
    onClick: (SearchHistoryKey) -> Unit,
) {
    SimpleFlexRow(
        space = 10.dp,
    ) {
        searchHotKeys.forEach {
            Box(
                modifier = Modifier
                    .background(
                        color = MaterialTheme.colorScheme.surfaceVariant,
                        shape = RoundedCornerShape(25.dp)
                    )
                    .clip(RoundedCornerShape(25.dp))
                    .clickable {
                        onClick(it)
                    }
            ) {
                Text(text = it.key, modifier = Modifier.padding(vertical = 5.dp, horizontal = 10.dp))
            }
        }
    }
}


@Composable
fun SearchHotKey(
    searchHotKeys: Async<List<HotSearchKey>>,
    onClick: (HotSearchKey) -> Unit
) {
    Column(
        modifier = Modifier.padding(horizontal = 12.dp)
    ) {
        Text(text = stringResource(id = Str.search_hot_key), modifier = Modifier.padding(bottom = 10.dp))
        when (searchHotKeys) {
            is Async.Success -> {
                SearchHotKeyContent(searchHotKeys) {
                    onClick(it)
                }
            }

            else -> {

            }
        }
    }
}

@Composable
private fun SearchHotKeyContent(
    searchHotKey: Async.Success<List<HotSearchKey>>,
    onClick: (HotSearchKey) -> Unit,
) {
    SimpleFlexRow(
        space = 10.dp,
    ) {
        searchHotKey.data.forEach {
            Box(
                modifier = Modifier
                    .background(
                        color = MaterialTheme.colorScheme.surfaceVariant,
                        shape = RoundedCornerShape(25.dp)
                    )
                    .clip(RoundedCornerShape(25.dp))
                    .clickable {
                        onClick(it)
                    }
            ) {
                Text(text = it.name, modifier = Modifier.padding(vertical = 5.dp, horizontal = 10.dp))
            }
        }
    }
}


